const path = require("path");
const webpack = require('webpack');
const AntDesignThemePlugin = require("antd-theme-webpack-plugin");
const options = {
    antDir: path.join(__dirname,'./node_modules/ant-design-vue ' ),
    stylesDir: path.join(__dirname, './ src ' ),
    varFile: path.join(__dirname,'./node_modules/ant-design-vue/lib/style/themes/default.less '),
    mainLessFile: "",
    themeVariables:[ '@primary-color'],
    generateOnce: false
}
const themePlugin = new AntDesignThemePlugin(options);

module.exports = {
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'primary-color': '#1DA57A',
                  },
                javascriptEnabled: true
            }
        }
    },
    configureWebpack: {
        plugins: [themePlugin, new webpack.IgnorePlugin(/^\.\/locale$/,/moment$/)],
        resolve: {
            alias: {
                "@ant-design/icons/lib/dist$" : path.resolve(__dirname,"./src/icons.js")
            }
        }
    },
    chainWebpack: config => {
        const svgRule = config.module.rule('svg')
        svgRule.uses.clear()
        svgRule.use('vue-svg-loader')
            .loader('vue-svg-loader');
    },
    devServer: {
        proxy: {
            'api': {
                target: 'http://localhost:3000',
                bypass: function(req, res) {
                    if(req.headers.accept.indexOf('html') !== -1) {
                        console.log('Skipping proxy for browser request');
                        return 'index.html';
                    }else if(process.env.MOCK !== "none"){
                        const name = req.path.split("/api/")[1].split("/").join("_");
                        const mock = require(`./mock/${name}`);
                        const result = mock(req.method);
                        delete require.cache[require.resolve(`./mock/${name}`)];//清除缓存，可以让每次拿到新的数据进而显示出来
                        return res.send(result);
                    }
                }
            }
        }
    }
}